package com.Rendering.UI.HUD;

import com.Gameplay.character.Role.parentRole;
import com.Utils.Clock;
import com.core.Window.Main.Game;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.effect.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;


public class HUD {
    //画笔的初始化
    private GraphicsContext gc;
    //血条精力条的绘制准备
    private double P1PreHeathy = 100;
    private double P2PreHeathy = 100;
    private double P1PreEnergy = 100;
    private double P2PreEnergy = 100;
    //时钟类对象
    private Clock renderClock;
    //血条框阴影设置
    private DropShadow redShadow;
    private DropShadow blueShadow;
    //光照设置
    private Lighting light;
    private Light.Point LeftPoint;
    private Light.Point RightPoint;

    //时间显示
    private Text Time;
    private String clockTime;

    public HUD(Canvas drawerCanvas) {

        renderClock = Game.getInstance().getClock();
        Time = new Text();

        this.gc = drawerCanvas.getGraphicsContext2D();
        Font SegmentFont = Font.loadFont(
                getClass().getResourceAsStream("/Fonts/Segment7.ttf"), 80
        );
        Time.setFont(SegmentFont);
        //阴影效果
        redShadow = new DropShadow();
        blueShadow = new DropShadow();

        redShadow.setColor(Color.rgb(221, 51, 51));
        redShadow.setRadius(20);
        redShadow.setBlurType(BlurType.GAUSSIAN);

        blueShadow.setColor(Color.valueOf("#0000CD"));
        blueShadow.setRadius(20);
        blueShadow.setBlurType(BlurType.GAUSSIAN);
        //光照效果设置
        light = new Lighting();
        LeftPoint = new Light.Point();
        RightPoint = new Light.Point();
        //设置漫反射程度
        light.setDiffuseConstant(1.5);
        // 设置光照的方向
        LeftPoint.setX(720);
        LeftPoint.setZ(300);
        RightPoint.setX(100);
        RightPoint.setZ(300);
        //设置光照颜色
        LeftPoint.setColor(Color.WHITE);
        RightPoint.setColor(Color.WHITE);


    }


    public void setTimeText() {
        clockTime = String.valueOf(renderClock.getClockTime());
        Time.setEffect(redShadow);
        Time.setX(670);
        Time.setY(80);
        Time.setFill(Color.RED);
        Time.setText(clockTime);


    }

    public void draw(parentRole P1, parentRole P2) {
        gc.clearRect(0, 0, 1440, 900);
        if (P2PreHeathy != P2.getHealth()) {
            P2PreHeathy = P2PreHeathy - (P2PreHeathy - P2.getHealth()) * 0.05;
            if (P2PreHeathy <= P2.getHealth()) {
                P2PreHeathy = P2.getHealth();
            }
        }
        if (P1PreHeathy != P1.getHealth()) {
            P1PreHeathy = P1PreHeathy - (P1PreHeathy - P1.getHealth()) * 0.03;
            if (P1PreHeathy <= P1.getHealth()) {
                P1PreHeathy = P1.getHealth();
            }
        }

        //虚拟血条绘制
        gc.setFill(Color.rgb(245, 95, 95));
        drawParallelogram(75, 75 + P1PreHeathy * 4.0D, 100 + P1PreHeathy * 4.0D, 100, 50, 25, true, false);
        drawParallelogram(965, 965 + P2PreHeathy * 4.0D, 940 + P2PreHeathy * 4.0D, 940, 50, 25, true, false);

        light.setLight(LeftPoint);
        gc.setEffect(light);


        //P1的实际血条绘制
        gc.setFill(Color.rgb(241, 41, 41));
        drawParallelogram(75, 75 + P1.getHealth() * 4.0D, 100 + P1.getHealth() * 4.0D, 100, 50, 25, true, false);

        //P2的实际血条绘制
        light.setLight(RightPoint);
        gc.setEffect(light);

        gc.setFill(Color.rgb(31, 104, 193));
        drawParallelogram(965, 965 + P2.getHealth() * 4.0D, 940 + P2.getHealth() * 4.0D, 940, 50, 25, true, false);

        //时钟绘制
        setTimeText();
        //设置血条框阴影
        gc.setEffect(redShadow);
        //P1和P2的血条框设置
        //画笔设置
        gc.setLineWidth(3);
        drawParallelogram(75, 75 + 400, 100 + 400, 100, 50, 25, false, true);
        gc.setEffect(blueShadow);
        drawParallelogram(965, 965 + 400, 940 + 400, 940, 50, 25, false, true);
        gc.setEffect(null);
    }

    public Text getTime() {
        return Time;
    }

    private void drawParallelogram(double x, double x1, double x2, double x3, double y, double height, boolean Filled, boolean Stroked) {


        // 开始绘制路径
        gc.beginPath();
        gc.moveTo(x, y); // 左上角
        gc.lineTo(x1, y); // 右上角
        gc.lineTo(x2, y + height); // 右下角
        gc.lineTo(x3, y + height); // 左下角
        gc.closePath(); // 闭合路径

        // 填充颜色
        if (Filled) {
            gc.fill();
        }

        // 如果需要描边
        if (Stroked) {
            gc.setStroke(Color.WHITE);
            gc.stroke();
        }
    }
}



